package com.car.action.admin;

import com.car.common.model.ImageQpulp;
import com.car.common.model.UsersInfo;
import com.car.intercept.AdminInterceptor;
import com.jfinal.aop.Before;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.utils.PicUtil;
import com.utils.StringUtil;
import com.utils.SystemClock;

@Before(AdminInterceptor.class)
public class PulpController extends Controller {
	
	
	public void cardList() {
		String sql = "select a.*,u.id as user_id,c.id as card_id,u.nick_name,u.user_mobile,CASE a.label  WHEN '1' THEN '性感' WHEN '2' THEN '正常' WHEN '0' THEN '色情'  ELSE '其他' END as label_name ";
		sql += ", FROM_UNIXTIME(a.create_time,'%Y-%m-%d %H:%i') as create_time_string,CASE a.status  WHEN '2' THEN '未处理' WHEN '1' THEN '系统已处理' WHEN '3' THEN '人工确认'  WHEN '4' THEN '人工删除' ELSE '其他' END as status_name ";
		String andSql = " from image_qpulp as a left join card c on c.id = a.relation_id left join users u on u.id = c.user_id where a.type =1 and c.id is not null and (a.status = 1 or  a.status = 2 )";
		if (!StringUtil.empty(getPara("label"))) {
			andSql += " and a.label = " + getPara("label");
		}
		if (!StringUtil.empty(getPara("status"))) {
			andSql += " and a.`status` = " + getPara("status");
		}
		andSql += " order by ";
	     //分数排序
        if(!StringUtil.empty(getPara("score"))){
        	andSql += getPara("score") + ", ";
        }
        //时间排序
        if(!StringUtil.empty(getPara("create_time"))){
        	andSql += getPara("create_time") + ", ";
        }
        andSql += " a.`status` desc,a.label asc,a.id desc ";
		int page = 1;
		if (!StringUtil.empty(getPara("page"))) {
			page = getParaToInt("page");
		}
		Page<ImageQpulp> dataPage = ImageQpulp.dao.paginate(page, 30, sql, andSql);
		for(ImageQpulp one: dataPage.getList()) {
			one.setImg(PicUtil.QINIU_DIR  + one.getImg());
		}
		setAttr("page", dataPage);
		setAttr("label", getPara("label"));
		setAttr("status", getPara("status"));
		setAttr("score", getPara("score"));
		setAttr("create_time", getPara("create_time"));
        render("card_list.html");
	}
	
	
	public void usersList() {
		String sql = "select a.*,u.id as user_id,u.nick_name,u.user_mobile,CASE a.label  WHEN '1' THEN '性感' WHEN '2' THEN '正常' WHEN '0' THEN '色情'  ELSE '其他' END as label_name ";
		sql += ", FROM_UNIXTIME(a.create_time,'%Y-%m-%d %H:%i') as create_time_string,CASE a.status  WHEN '2' THEN '未处理' WHEN '1' THEN '系统已处理' WHEN '3' THEN '人工确认'  WHEN '4' THEN '人工删除' ELSE '其他' END as status_name ";
		String andSql = " from image_qpulp as a  left join users u on u.id = a.relation_id where a.type = 2 and (a.status = 1 or  a.status = 2) ";
		if (!StringUtil.empty(getPara("label"))) {
			andSql += " and a.label = " + getPara("label");
		}
		if (!StringUtil.empty(getPara("status"))) {
			andSql += " and a.status = " + getPara("status");
		}
		andSql += " order by ";
	     //分数排序
		if(!StringUtil.empty(getPara("score"))){
       		andSql += getPara("score") + ", ";
       	}
       	//时间排序
        if(!StringUtil.empty(getPara("create_time"))){
       	 	andSql += getPara("create_time") + ", ";
        }
        andSql += " a.`status` desc,a.label asc,a.id desc ";
		int page = 1;
		if (!StringUtil.empty(getPara("page"))) {
			page = getParaToInt("page");
		}
		Page<ImageQpulp> dataPage = ImageQpulp.dao.paginate(page, 100, sql, andSql);
		for(ImageQpulp one: dataPage.getList()) {
			one.setImg(PicUtil.QINIU_DIR  + one.getImg());
		}
		setAttr("page", dataPage);
		setAttr("label", getPara("label"));
		setAttr("status", getPara("status"));
        render("users_list.html");
	}
	
	
	public void usersbgList() {
		String sql = "select a.*,u.id as user_id,u.nick_name,u.user_mobile,CASE a.label  WHEN '1' THEN '性感' WHEN '2' THEN '正常' WHEN '0' THEN '色情'  ELSE '其他' END as label_name ";
		sql += ", FROM_UNIXTIME(a.create_time,'%Y-%m-%d %H:%i') as create_time_string,CASE a.status  WHEN '2' THEN '未处理' WHEN '1' THEN '系统已处理' WHEN '3' THEN '人工确认'  WHEN '4' THEN '人工删除' ELSE '其他' END as status_name ";
		String andSql = " from image_qpulp as a  left join users u on u.id = a.relation_id where a.type = 3 and (a.status = 1 or  a.status = 2) ";
		if (!StringUtil.empty(getPara("label"))) {
			andSql += " and a.label = " + getPara("label");
		}
		if (!StringUtil.empty(getPara("status"))) {
			andSql += " and a.status = " + getPara("status");
		}
		andSql += " order by a.id desc, a.status desc,a.label asc ";
		int page = 1;
		if (!StringUtil.empty(getPara("page"))) {
			page = getParaToInt("page");
		}
		Page<ImageQpulp> dataPage = ImageQpulp.dao.paginate(page, 30, sql, andSql);
		for(ImageQpulp one: dataPage.getList()) {
			one.setImg(PicUtil.QINIU_DIR  + one.getImg());
		}
		setAttr("page", dataPage);
		setAttr("label", getPara("label"));
		setAttr("status", getPara("status"));
        render("usersbg_list.html");
	}
	
	public void albumList() {
		String sql = "select a.*,u.id as user_id,u.nick_name,u.user_mobile,CASE a.label  WHEN '1' THEN '性感' WHEN '2' THEN '正常' WHEN '0' THEN '色情'  ELSE '其他' END as label_name ";
		sql += ", FROM_UNIXTIME(a.create_time,'%Y-%m-%d %H:%i') as create_time_string,CASE a.status  WHEN '2' THEN '未处理' WHEN '1' THEN '系统已处理' WHEN '3' THEN '人工确认'  WHEN '4' THEN '人工删除' ELSE '其他' END as status_name ";
		String andSql = " from image_qpulp as a left join user_albums ua on ua.id = a.relation_id  left join users u on u.id = ua.user_id where a.type = 4 and (a.status = 1 or  a.status = 2) ";
		if (!StringUtil.empty(getPara("label"))) {
			andSql += " and a.label = " + getPara("label");
		}
		if (!StringUtil.empty(getPara("status"))) {
			andSql += " and a.status = " + getPara("status");
		}
		andSql += " order by a.id desc, a.status desc,a.label asc ";
		int page = 1;
		if (!StringUtil.empty(getPara("page"))) {
			page = getParaToInt("page");
		}
		Page<ImageQpulp> dataPage = ImageQpulp.dao.paginate(page, 30, sql, andSql);
		for(ImageQpulp one: dataPage.getList()) {
			one.setImg(PicUtil.QINIU_DIR  + one.getImg());
		}
		setAttr("page", dataPage);
		setAttr("label", getPara("label"));
		setAttr("status", getPara("status"));
        render("album_list.html");
	}
	
	
    /**
     * 人工确认(删除记录)
     */
    public void pass(){
    	String ids = getPara("ids");
    	String[] idsArr = ids.split(",");
    	for(String id : idsArr) {
    		ImageQpulp one = ImageQpulp.dao.findById(id);
    		Db.update("update image_qpulp set status = 3 ,delete_time = " + SystemClock.now()/1000L + " where id =" + id);
        	new PicUtil().delete(one.getImg());
        	
    	}
    	renderText("操作成功!");
    }
    
    /**
     * 替换(不通过)
     */
    public void unpass(){
    	String ids = getPara("ids");
    	String[] idsArr = ids.split(",");
    	ImageQpulp.dao.unpass(idsArr, true);
    	renderText("操作成功!");
    }
    
    /**
     * 还原
     */
    public void restored(){
    	String ids = getPara("ids");
    	String[] idsArr = ids.split(",");
    	ImageQpulp.dao.restored(idsArr);
    	renderText("操作成功!");
    }
    
}
